<!--
 * @Description: 工程策划表
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2022-03-17 15:35:08
 * @LastEditTime: 2022-12-06 10:15:30
 * @FilePath: \cscec-pms-admin\src\views\project\plannManagement\plannTableSummary\plannTableSummaryView.vue
-->
<template>
  <base-breadcrumb :title="breadcrumb" :breadcrumb="!isAudit">
    <a-card>
      <title-name title="基本信息" />
      <custom-form
        ref="form"
        :is-validate="true"
        :edit-model="''"
        :form="form"
        :form-list="formOption"
      />
    </a-card>
    <a-card>
      <title-name title="策划明细表" />
      <s-table
        ref="table"
        row-key="id"
        :columns="columns1"
        :data="loadData"
        :alert="true"
        show-size-changer
        show-quick-jumper
        bordered
        :show-pagination="false"
      >
        <template
          slot="version"
          slot-scope="text,record"
        >
          <span v-if="text < 1">--</span>
          <a
            v-else
            @click="handleVersionRecord(record)"
          >
            V
            {{ text }}
          </a>
        </template>
        <template
          slot="operation"
          slot-scope="text, record"
        >
          <MoreButton>
            <a @click="handleOperation('detail',record)">查看</a>
            <a
              v-if="record.type === 0"
              @click="$bus.$emit('showApprovalDetailsDialog', record.id)"
            >审批信息</a>
          </MoreButton>
        </template>
      </s-table>
    </a-card>
    <a-card>
      <title-name title="附件" />
      <UploadTableNew
        ref="uploadTable"
        :mode="file.mode"
        :init-file-list="file.fileList"
        :need-page="file.needPage"
        :use-add="pageType == 'update'"
        :use-delete="pageType === 'update'"
        :business-code="file.businessCode"
      />
    </a-card>
    <div v-if="!isConsultation" class="page-btn-right-top">
      <a-button
        v-if="pageType === 'update'"
        type="primary"
        :loading="submiting"
        @click="handleSubmit(0)"
        style="margin-right: 10px"
      >
        保存草稿
      </a-button>
      <a-button
        v-if="pageType === 'update'"
        type="primary"
        :loading="submiting"
        @click="handleSubmit(1)"
        style="margin-right: 10px"
      >
        提交
      </a-button>
      <a-button @click="$close">
        关闭
      </a-button>
    </div>
    <!-- 版本记录 -->
    <VersionRecord
      :visible.sync="versionVisible"
      :data="versionData"
    />
    <!-- 版本记录 -->
    <!-- 选择审批人 -->
    <selectApprover
      :business-id="businessId"
      :process-config="setProcessConfig"
    />
    <!-- 选择审批人 -->
  </base-breadcrumb>
</template>
<script>
import { formOption, columns1 } from './model.js'
import MoreButton from '@/components/MoreButton'
import VersionRecord from '../../projectManagementPlanning/projectManagementPlanningSchedule/components/VersionRecord'
import { view } from '@/api/project/plannManagement/plannTableSummary'
import { pageName } from '../../projectManagementPlanning/projectManagementPlanningComponents/components/model.js'
import { pageName as pageName1 } from '../plannTable/model.js'
import Vue from 'vue'
export default {
  name: 'PlannTableSummaryView' + Date.now(),
  props: {
    // 是否作为审批详情
    isAudit: {
      type: Boolean,
      default: false
    },
    // 是否作为意见征询
    isConsultation: {
      type: Boolean,
      default: false
    }
  },
  components: {
    MoreButton,
    VersionRecord
  },
  data() {
    return {
      // 面包屑名字
      breadcrumb: '',
      // 基本信息表单
      form: {},
      // 搜索选项
      formOption,
      // 版本记录显示隐藏
      versionVisible: false,
      // 版本记录信息
      versionData: {},
      // 表头
      columns1,
      // 附件
      file: {
        // 处理操作的模式：'add'模式下，删除操作会调用文件删除接口，'edit'模式只会在表格中移除删除数据，不会调用接口
        mode: 'add',
        // 上传文件的业务编码
        businessCode: 'PLAN02',
        // 是否需要分页
        needPage: false,
        // 初始化文件列表
        fileList: []
      },
      // 提交
      submiting: false,
      // 当前页面类型
      pageType: this.$route.query.pageType || '',
      // 当前id
      id: this.$route.query.id || '',
      // 当前登录人信息
      info: Vue.getAcloudProvider().getUserInfo(),
      // 流程配置
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      // 业务id
      businessId: null
    }
  },
  async created() {
    //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('scheme_process')
  },
  mounted() {
    this.setBreadcrumb()
  },
  methods: {
    /**
     * @description: 设置面包屑名字
     * @param {void}
     * @return {void}
     */
    setBreadcrumb() {
      const text = {
        detail: '查看',
        update: '编辑'
      }
      this.breadcrumb = `${text[this.pageType]}策划执行书汇总`
    },
    /**
     * @description: 版本记录
     * @param {object} record 行数据
     * @return {void}
     */
    handleVersionRecord(record) {
      this.versionVisible = true
      this.versionData = record
      this.versionData.isAudit = this.isAudit
    },
    /**
     * @description: 表格数据加载方法
     * @param {void}
     * @return {promise} 接口Promise
     */
    async loadData() {
      try {
        const res = await view.querySchemeSummaryDetail(this.id)
        if (res.code === 200) {
          const { branchName, projectName, projectCode, fillName, fillTime, fileList, projectId } = res.data
          this.form = {
            branchName,
            projectName,
            projectCode,
            fillName,
            fillTime,
            projectId
          }
          this.file.fileList = fileList || []
          if (!fillName) {
            this.form.fillName = this.info.name
          }
          if (!fillTime) {
            this.form.fillTime = this.moment().format('YYYY-MM-DD')
          }
          return Promise.resolve({ code: 200, data: res.data.detailList })
        } else {
          return Promise.reject()
        }
      } catch (error) {
        console.log(error)
        return Promise.reject()
      }
    },
    /**
     * @description: 操作
     * @param {string} pageType 操作类型
     * @param {object} record 行数据
     * @return {void}
     */
    handleOperation(pageType, record) {
      const { id, code, type } = record
      // 通过type判断 type为1是工程策划新的表，否则是项目策划的表
      if (type === 0) {
        // 项目策划
        if (this.isAudit) {
          let routeData = this.$router.resolve({
            path: '/process/auditBusinessDetail',
            query: {
              detailUrl: '/project/projectManagementPlanning/projectManagementPlanningComponents',
              id,
              pageType,
              editModel: pageType == 'detail' ? 0 : 1,
              pageName: pageName[code - 1]
            }
          })
          window.open(routeData.href, '_blank')
        } else {
          this.$router.push({
            path: '/project/projectManagementPlanning/projectManagementPlanningComponents',
            query: {
              id,
              pageType,
              editModel: pageType == 'detail' ? 0 : 1,
              pageName: pageName[code - 1]
            }
          })
        }
      } else {
        // 工程策划
        if (this.isAudit) {
          let routeData = this.$router.resolve({
            path: '/process/auditBusinessDetail',
            query: {
              detailUrl: '/project/plannManagement/plannTableComponents',
              id,
              pageType,
              editModel: pageType == 'detail' ? 0 : 1,
              pageName: pageName1[code - 26]
            }
          })
          window.open(routeData.href, '_blank')
        } else {
          this.$router.push({
            path: '/project/plannManagement/plannTableComponents',
            query: {
              id,
              pageType,
              editModel: pageType == 'detail' ? 0 : 1,
              pageName: pageName1[code - 26]
            }
          })
        }
      }
    },
    /**
     * @description: 提交
     * @param {number} status 状态0保存草稿，1提交
     * @return {void}
     */
    handleSubmit(status) {
      if (status === 1) {
        if (this.file.fileList.length > 0) {
          //测试喊屏蔽的
          // this.$confirm({
          //   title: '温馨提示',
          //   content: '是否确定提交?',
          //   onOk() {
          this.handleFun(status)
          //   }
          // })
        } else {
          this.$message.warning('请上传附件')
        }
      } else {
        this.handleFun(status)
      }
    },
    /**
     * @description: 提交接口
     * @param {number} status 状态0保存草稿，1提交
     * @return {void}
     */
    handleFun(status) {
      this.submiting = true
      this.setProcessConfig = null
      this.submitApprovalConfirm(status===1 ? this.processConfig:6, (flag)=>{
        if(!flag) {
          this.submiting = false
          return
        }
        view.submitScheme({
                  id: this.id,
                  status,
                  fileList: this.file.fileList
                })
                .then(({ code, data }) => {
                  if (code === 200) {
                    this.$message.success('提交成功')
                    if (status === 1) {
                      this.businessId = data
                      this.setProcessConfig = this.processConfig
                    } else {
                      this.$close()
                    }
                  }
                })
                .finally(() => {
                  this.submiting = false
                })
      })

    }
  }
}
</script>
<style lang="scss">
</style>
